//
//  UIView+YJDrawCategory.m
//  BezierDemo
//
//  Created by 申元江 on 2018/12/27.
//  Copyright © 2018 yuanjiang. All rights reserved.
//

#import "UIView+YJDrawCategory.h"

@implementation UIView (YJDrawCategory)

///draw line in view
-(void)yj_drawLineFromPoint:(CGPoint)fPoint
                    toPoint:(CGPoint)tpoint
                  lineColor:(UIColor *)color
                 lineHeight:(CGFloat)height
{
    ///strokeColor 描边颜色
    ///fillcolor 填充颜色
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    if (color) {
        shapeLayer.strokeColor = color.CGColor;
    }
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    
    shapeLayer.path = ({
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:fPoint];
        [path addLineToPoint:tpoint];
        path.CGPath;
    });
    shapeLayer.lineWidth = height;
    [self.layer addSublayer:shapeLayer];
}

/// draw dash line in view type: 0-cube, 1-round
-(void)yj_drawDashLineFrompoint:(CGPoint)fPoint
                        toPoint:(CGPoint)tPoint
                      lineColor:(UIColor *)color
                      lineWidth:(CGFloat)width
                     lineHeight:(CGFloat)height
                      lineSpace:(CGFloat)space
                       lineType:(NSInteger)type
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    if (color) {
        shapeLayer.strokeColor =  color.CGColor;
    }
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.path = ({
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:fPoint];
        [path addLineToPoint:tPoint];
        path.CGPath;
    });
    //第一格虚线缩进多少  - the degree of indent of the  first cell
    //shapeLayer.lineDashPhase = 4;
    //虚线的高 属性为lineWidth
    shapeLayer.lineWidth = height < 0 ? 1 : height;
    //layer的两端样式
    shapeLayer.lineCap = kCALineCapButt;
    width = width < 0 ? 1: width;
    shapeLayer.lineDashPattern = @[@(width),@(space)];
    if (type == 1) {
        shapeLayer.lineCap = kCALineCapRound;
        shapeLayer.lineDashPattern = @[@(width),@(space+width)];
    }
    [self.layer addSublayer:shapeLayer];
}

///draw pentagram in view rate： 0.3 ~ 1.1
-(void)yj_drawPentagram:(CGPoint)center
                 radius:(CGFloat)radius
                  color:(UIColor *)color
                   rate:(CGFloat)rate
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor clearColor].CGColor;
    shapeLayer.fillColor = [UIColor orangeColor].CGColor;
    if (color) {
        shapeLayer.fillColor = color.CGColor;
    }
    shapeLayer.path = ({
        UIBezierPath *path = [UIBezierPath bezierPath];
        //五角星最上面的点
        CGPoint first = CGPointMake(center.x, center.y-radius);
        [path moveToPoint:first];
        
        //点与点之间夹角为 2 * M_PI/5.0,要隔一个点才连线
        CGFloat angle = 4 * M_PI/5.0;
        if (rate > 1.5) {
            rate  = 1.5;
        }
        for (int i = 1; i<=5; i++) {
            CGFloat x  = center.x - sinf(i*angle)*radius;
            CGFloat y  = center.y - cosf(i*angle)*radius;
            
            CGFloat midx = center.x  - sinf(i*angle-2*M_PI/5.0)*radius*rate;
            CGFloat midy = center.y - cosf(i *angle-2*M_PI/5.0)*radius*rate;
            [path addQuadCurveToPoint:CGPointMake(x, y) controlPoint:CGPointMake(midx, midy)];
        }
        path.CGPath;
    });
    shapeLayer.lineWidth = 1.0f;
    shapeLayer.lineJoin = kCALineJoinRound;
    [self.layer addSublayer:shapeLayer];

}

///draw rect. type: 0 - cube, 1-round
-(void)yj_drawRect:(CGRect)rect
         lineColor:(UIColor *)color
         lineWidth:(CGFloat)width
        lineHeight:(CGFloat)height
          lineType:(NSInteger)type
            isDash:(BOOL)dash
         lineSpace:(CGFloat)space
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    if (color) {
        shapeLayer.strokeColor  = color.CGColor;
    }
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.path = [UIBezierPath bezierPathWithRect:rect].CGPath;
    shapeLayer.lineWidth = height < 0 ? 1 : height;
    shapeLayer.lineCap = kCALineCapButt;
    if (type == 1) {
        shapeLayer.lineCap = kCALineCapRound;
    }
    width = width < 0 ? 1 : width;
    if (dash) {
        shapeLayer.lineDashPattern = @[@(width),@(space)];
        if (type == 1) {
            shapeLayer.lineDashPattern = @[@(width),@(space + width)];
        }
    }
    [self.layer addSublayer:shapeLayer];
    
}

@end

